﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using He.BLL;
using He.Web.UI;
using He.Common;

namespace He.Web.manage.network
{
    public partial class edit : He.Web.UI.Manage
    {
        private string action = Enums.ActionEnum.Add.ToString(); //操作类型
        public int id;
        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = GetString("action");
            if (!string.IsNullOrEmpty(_action) && _action == Enums.ActionEnum.Edit.ToString())
            {
                this.action = Enums.ActionEnum.Edit.ToString();//修改类型
                if (!int.TryParse(Request.QueryString["id"] as string, out this.id))
                {
                    JscriptMsg(350, 230, "错误提示", "<b>出现错误啦！</b>您要修改的信息不存在或参数不正确。", "back", "Error");
                    return;
                }
                if (!new He.BLL.network().Exists(this.id))
                {
                    JscriptMsg(350, 230, "错误提示", "信息不存在或已被删除！", "back", "Error");
                    return;
                }
            }

            if (!IsPostBack)
            {
                if (action == Enums.ActionEnum.Edit.ToString())
                {
                    chkLoginLevel("editnetwork");

                    ShowInfo(this.id);
                }
                else
                {
                    chkLoginLevel("addnetwork");
                }
            }
        }

        #region 赋值操作=================================
        protected void ShowInfo(int _id)
        {
            He.Model.network model = new BLL.network().GetModel(_id);
            txtTitle.Text = model.title;
            ddl_area.SelectedValue = model.province;
            txtIntrodune.Text = model.content;
            chkIsShow.Checked = model.is_show == 1 ? true : false;
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            BLL.network bll = new BLL.network();
            Model.network model = new Model.network();
            model.title = txtTitle.Text.Trim().Replace("'", "''").Replace("''", " ");
            model.province = ddl_area.SelectedValue;
            model.content = txtIntrodune.Text.Replace("'", "''").Replace("''", " ");
            model.is_show = chkIsShow.Checked ? 1 : 0;
            model.lang = lang;
            if (bll.Add(model) > 0)
            {
                base.WriteNetworkXml();
                //保存日志
                SaveLogs(string.Format("[营销网络管理]增加内容:{0}", model.title));
                return true;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            BLL.network bll = new BLL.network();
            Model.network model = new BLL.network().GetModel(_id);
            model.title = txtTitle.Text.Trim().Replace("'", "''").Replace("''", " ");
            model.province = ddl_area.SelectedValue;
            model.content = txtIntrodune.Text.Replace("'", "''").Replace("''", " ");
            model.is_show = chkIsShow.Checked ? 1 : 0;
            if (bll.Update(model))
            {
                base.WriteNetworkXml();
                //保存日志
                SaveLogs(string.Format("[营销网络管理]修改内容:{0}", model.title));
                return true;
            }
            return false;
        }
        #endregion

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (action == Enums.ActionEnum.Edit.ToString()) //修改
            {
                string url = CookieHelper.Get("LastListUrl");
                if (string.IsNullOrEmpty(url))
                {
                    url = Request.RawUrl;
                }

                if (!DoEdit(this.id))
                {
                    JscriptPrint("保存过程中发生错误啦！", url, "Error");
                    return;
                }
                JscriptPrint("修改信息成功啦！", url, "Success");
            }
            else //添加
            {
                if (!DoAdd())
                {
                    JscriptPrint("保存过程中发生错误啦！", "edit.aspx?lang=" + lang, "Error");
                    return;
                }
                JscriptPrint("添加信息成功啦！", "edit.aspx?lang=" + lang, "Success");
            }
        }
    }
}
